﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using NLog;
using OPCService.Schedulers;

namespace OPCService
{
    static class Program
    {
        private static readonly Logger logger = NLog.LogManager.GetCurrentClassLogger();
        private static SchedulerManager schedulerManager = new SchedulerManager();

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // 启动调度器（在后台线程中异步执行）
            Task.Run(async () =>
            {
                try
                {
                    logger.Info("调度任务开始启动...");
                    await schedulerManager.StartAsync();
                    await schedulerManager.ScheduleJobsAsync();
                    logger.Info("所有调度任务已启动");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"调度器初始化失败: {ex.Message}");
                }
            });

            Application.Run(new ACCS());

        }

        public static async Task OnApplicationExitAsync()
        {
            if (schedulerManager != null)
            {
                try
                {
                    await schedulerManager.ShutdownAsync();
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"关闭调度器时出错: {ex.Message}");
                }
            }
        }

    }
}
